package com.git.hui.yinshua.biz.goods.dao.repository;

import com.git.hui.yinshua.api.model.ExceptionUtil;
import com.git.hui.yinshua.api.model.Status;
import com.git.hui.yinshua.api.model.constants.GlobalConstants;
import com.git.hui.yinshua.biz.goods.dao.domain.GoodsAttributeKeyEntity;
import com.git.hui.yinshua.biz.goods.dao.domain.GoodsAttributeValueEntity;
import com.git.hui.yinshua.biz.goods.dao.mapper.AttributeMapper;
import com.git.hui.yinshua.biz.rest.models.goods.req.AttrKeyReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品属性dao
 *
 * @author YiHui
 * @date 2024/6/13
 */
@Repository
public class AttributeRepository {

    @Autowired
    private AttributeMapper attributeMapper;

    public GoodsAttributeKeyEntity addAttributeKey(AttrKeyReq key) {
        GoodsAttributeKeyEntity dbRecord = attributeMapper.getByKey(key.getKey());
        if (dbRecord != null) {
            throw ExceptionUtil.of(Status.StatusEnum.RECORDS_ALREADY_EXISTS, key.getKey());
        }

        GoodsAttributeKeyEntity entity = new GoodsAttributeKeyEntity();
        entity.setAttributeName(key.getKey());
        entity.setAttributeUnit(key.getUnit() == null ? "" : key.getUnit());
        entity.setCategoryId(key.getCid() == null ? 0 : key.getCid());
        attributeMapper.saveAttributeKey(entity);
        return entity;
    }

    public GoodsAttributeKeyEntity updateAttributeKey(AttrKeyReq key) {
        if (StringUtils.isNotBlank(key.getKey())) {
            // 属性名不能重复
            GoodsAttributeKeyEntity dbRecord = attributeMapper.getByKey(key.getKey());
            if (!dbRecord.getId().equals(key.getKeyId())) {
                throw ExceptionUtil.of(Status.StatusEnum.RECORDS_ALREADY_EXISTS, key.getKey());
            }
        }

        GoodsAttributeKeyEntity entity = new GoodsAttributeKeyEntity();
        entity.setId(key.getKeyId());
        if (key.getCid() != null) {
            entity.setCategoryId(key.getCid());
        }
        if (key.getKey() != null) {
            entity.setAttributeName(key.getKey());
        }
        if (key.getUnit() != null) {
            entity.setAttributeUnit(key.getUnit());
        }
        attributeMapper.updateAttributeKey(entity);
        return entity;
    }

    /**
     * 查询所有分类
     *
     * @return
     */
    public List<GoodsAttributeKeyEntity> listAllKeys() {
        return attributeMapper.listKey(null);
    }

    /**
     * 查询适用某个分类的属性，用于添加商品指定属性规格时适用
     *
     * @param categoryId 商品分类
     * @return
     */
    public List<GoodsAttributeKeyEntity> listKeysForCid(Integer categoryId) {
        List<Integer> cids = new ArrayList<>(2);
        if (categoryId != null) {
            cids.add(categoryId);
        }
        cids.add(GlobalConstants.AttrApi.APPLY_FOR_ALL_CID_KEYS);
        return attributeMapper.listKey(cids);
    }

    public boolean deleteAttributeKey(Integer key) {
        return attributeMapper.deleteAttributeKey(key) > 0;
    }

    //  ===================== 属性值 ==========================================

    public GoodsAttributeValueEntity addAttributeValue(Integer keyId, String value) {
        GoodsAttributeValueEntity entity = new GoodsAttributeValueEntity();
        entity.setAttributeId(keyId);
        entity.setAttributeValue(value);
        attributeMapper.saveAttributeValue(entity);
        return entity;
    }

    public boolean updateAttributeValue(Integer valId, String value) {
        return attributeMapper.updateAttributeValue(valId, value) > 0;
    }

    public List<GoodsAttributeValueEntity> listValuesByKey(Integer keyId) {
        return attributeMapper.listValueByKey(keyId);
    }

    public boolean deleteAttributeValue(Integer valId) {
        return attributeMapper.deleteAttributeValue(valId) > 0;
    }
}
